package api

import (
	"OJCenter/global"
	"OJCenter/idl"
	"OJCenter/provider"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"github.com/google/uuid"
	"net/http"
	"strconv"
)

type SubReq struct {
	Code string
	Sub  idl.Submission
}

// @Summary	添加提交
// @Produce	json
// @Param		sub	body		SubReq			true	"提交信息"
// @Success	200	{object}	global.Response	""
// @Fail		400  {object} global.Response ""
// @Router		/api/v1/createSubmission [post]
func (a *Api) CreateSubmission(c *gin.Context) {
	var Sr SubReq

	err := c.ShouldBindJSON(&Sr)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	validate := validator.New()
	err = validate.Struct(&Sr)
	if err != nil {
		global.MarkErrors(err.(validator.ValidationErrors))
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
		return
	}

	Uuid, _ := uuid.NewRandom()
	Sr.Sub.Sid = int64(Uuid.ID())

	problem := idl.Problem{Pid: Sr.Sub.Pid}
	ps, err := a.M.SelectProblem(problem)
	if err != nil {
		return
	}

	jm := idl.JMessage{
		Sid:        strconv.Itoa(int(Sr.Sub.Sid)),
		Src:        Sr.Code,
		Language:   Sr.Sub.Language,
		MaxCPUTime: (*ps)[0].TimeLimit,
		MaxMemory:  (*ps)[0].MemoryLimit,
		TestCaseID: strconv.Itoa(int((*ps)[0].Pid)),
	}
	provider.SendJMessage(jm)

	err = a.M.SubProblem(&Sr.Sub)

	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	global.Res(c, http.StatusOK, global.SUCCESS, nil)
}

// @Summary	获取提交列表
// @Produce	json
// @Param		uid	query		int				true	"uid"
// @Param		cid	query		int				true	"cid"
// @Param		pid	query		int				true	"pid"
// @Success	200	{object}	global.Response	""
// @Fail		400  {object} global.Response{Data=[]idl.Submission} ""
// @Router		/api/v1/getSubmissionListInContest [Get]
func (a *Api) GetSubmissionListInContest(c *gin.Context) {
	uid := c.Query("uid")
	cid := c.Query("cid")
	pid := c.Query("pid")
	_uid, err := strconv.Atoi(uid)
	_cid, err := strconv.Atoi(cid)
	_pid, err := strconv.Atoi(pid)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}

	ret, err := a.M.GetSubmissionInContest(int64(_uid), int64(_cid), int64(_pid))
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
		return
	}
	global.Res(c, http.StatusOK, global.SUCCESS, *ret)
}

// @Summary	获取提交列表
// @Produce	json
// @Param		uid	query		int				true	"uid"
// @Param		cid	query		int				true	"cid"
// @Success	200	{object}	global.Response	""
// @Fail		400  {object} global.Response{Data=[]idl.Submission} ""
// @Router		/api/v1/getSubmissionList [Get]
func (a *Api) GetSubmissionList(c *gin.Context) {

	uid := c.Query("uid")
	cid := c.Query("cid")
	_uid, err := strconv.Atoi(uid)
	_cid, err := strconv.Atoi(cid)
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, err)
		return
	}
	ret, err := a.M.GetSubmission(int64(_uid), int64(_cid))
	if err != nil {
		global.Res(c, http.StatusBadRequest, global.INVALID_PARAMS, nil)
		return
	}
	global.Res(c, http.StatusOK, global.SUCCESS, *ret)
}
